<?php
# 文件名称:response.php
# MetInfo在线反馈系统 
# Copyright (C) 长沙米拓信息技术有限公司 (http://www.metinfo.cn). All rights reserved.
defined( '_SYS' ) or die( 'No direct script access allowed' );
$GLOBALS['__RESPONSE'] = new stdClass();
$GLOBALS['__RESPONSE']->cachable = false;
$GLOBALS['__RESPONSE']->headers  = array();
$GLOBALS['__RESPONSE']->body	 = array();

class C_response
{
	/**
	 * 是否缓存
	 */
	function allowCache($allow = null)
	{
		if (!is_null($allow)) {
			$GLOBALS['__RESPONSE']->cachable = (bool) $allow;
		}
		return $GLOBALS['__RESPONSE']->cachable;
	}
	/**
	 * 文件流头部
	 *
	 */
	function setHeader($name, $value, $replace = false)
	{
		$name	= (string) $name;
		$value	= (string) $value;
		if ($replace)
		{
			foreach ($GLOBALS['__RESPONSE']->headers as $key => $header) {
				if ($name == $header['name']) {
					unset($GLOBALS['__RESPONSE']->headers[$key]);
				}
			}
		}
		$GLOBALS['__RESPONSE']->headers[] = array(
			'name'	=> $name,
			'value'	=> $value
		);
	}
	function getHeaders() {
		return  $GLOBALS['__RESPONSE']->headers;
	}
	function clearHeaders() {
		$GLOBALS['__RESPONSE']->headers = array();
	}
	/**
	 * 发送文件流头部
	 *
	 */
	function sendHeaders()
	{
		if (!headers_sent())
		{
			foreach ($GLOBALS['__RESPONSE']->headers as $header)
			{
				if ('status' == strtolower($header['name']))
				{
					// 'status' headers indicate an HTTP status, and need to be handled slightly differently
					header(ucfirst(strtolower($header['name'])) . ': ' . $header['value'], null, (int) $header['value']);
				} else {
					header($header['name'] . ': ' . $header['value']);
				}
			}
		}
	}
	function setBody($content) {
		$GLOBALS['__RESPONSE']->body = array((string) $content);
	}
	 /**
	 * 添加到第一个
	 *
	 */
	function prependBody($content) {
		array_unshift($GLOBALS['__RESPONSE']->body, (string) $content);
	}
	/**
	 * 追加到尾部
	 */
	function appendBody($content) {
		array_push($GLOBALS['__RESPONSE']->body, (string) $content);
	}
	function getBody($toArray = false)
	{
		if ($toArray) {
			return $GLOBALS['__RESPONSE']->body;
		}
		ob_start();
		foreach ($GLOBALS['__RESPONSE']->body as $content) {
			echo $content;
		}
		return ob_get_clean();
	}
	function toString($compress = false)
	{
		$data = C_response::getBody();
		// Don't compress something if the server is going todo it anyway. Waste of time.
		if($compress && !ini_get('zlib.output_compression') && ini_get('output_handler')!='ob_gzhandler') {
			$data = C_response::_compress($data);
		}
		if (C_response::allowCache() === false)
		{
			C_response::setHeader( 'Expires', 'Mon, 1 Jan 2009 00:00:00 GMT', true ); 				// Expires in the past
			C_response::setHeader( 'Last-Modified', gmdate("D, d M Y H:i:s") . ' GMT', true ); 		// Always modified
			C_response::setHeader( 'Cache-Control', 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0', false );
			C_response::setHeader( 'Pragma', 'no-cache' ); 											// HTTP 1.0
		}
		C_response::sendHeaders();
		return $data;
	}

	function _compress( $data )
	{
		$encoding = C_response::_clientEncoding();
		if (!$encoding)
			return $data;
		if (!extension_loaded('zlib') || ini_get('zlib.output_compression')) {
			return $data;
		}
		if (headers_sent())
			return $data;
		if (connection_status() !== 0)
			return $data;

		$level = 4; //ideal level
		/*
		$size		= strlen($data);
		$crc		= crc32($data);
		$gzdata		= "\x1f\x8b\x08\x00\x00\x00\x00\x00";
		$gzdata		.= gzcompress($data, $level);
		$gzdata 	= substr($gzdata, 0, strlen($gzdata) - 4);
		$gzdata 	.= pack("V",$crc) . pack("V", $size);
		*/
		$gzdata = gzencode($data, $level);
		C_response::setHeader('Content-Encoding', $encoding);
		C_response::setHeader('X-Content-Encoded-By', 'Joomla! 1.5');
		return $gzdata;
	}
	function _clientEncoding()
	{
		if (!isset($_SERVER['HTTP_ACCEPT_ENCODING'])) {
			return false;
		}
		$encoding = false;
		if (false !== strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) {
			$encoding = 'gzip';
		}
		if (false !== strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'x-gzip')) {
			$encoding = 'x-gzip';
		}
		return $encoding;
	}
}
# 本程序是一个开源系统,使用时请你仔细阅读使用协议,商业用途请自觉购买商业授权.
# Copyright (C) 长沙米拓信息技术有限公司 (http://www.metinfo.cn). All rights reserved.
?>
